Okay.
So today we're going to take a look at user input service.
Now we looked last lesson about the possibilities that you can do with services okay.
Services are absolutely amazing I really like them okay.
And user input service is another one of those services which I think are extremely cool.
Okay.
Now User Input Service allows you to take more advanced input from the player.
So at the moment player input consists of players touching parts, players left clicking using click
detectors, and players pressing and holding down keys.
Um, using um proximity prompts.
Okay, don't know why it's took me that long to think about it, but um, so they're all pretty cool.
But, um, how do people make cars and how do people make other kind of things which require more precise
input?
There's no button and no clicking involved in making a car.
It's you're using different keys and things like that, and also lots of other applications as well,
like pressing a, pressing a key on your keyboard to open a door or to open up your inventory or to
sprint, for example.
These are all examples.
These are all uses of user input service, and it really allows you to take more stuff from the player.
Okay.
Um, so user input service because we're taking input from the player on your keyboard, on your mouse,
on other things.
We can be using a local script for this.
Okay.
So go over to start player scripts and add in a local script okay.
Lack of any service.
We first need to get the service.
So in this time we're going to reference user input service.
And if equal to our game, cut on get service.
User input service, and you will use this code quite a bit, so you will get used to writing that out.
Okay.
So, um, user input service.
It has quite a few uses.
So it has a quite, quite a few things to do with it.
But the main two things are two events that I want to show you.
Okay.
I will mention the other things that I think this is useful for, but the main use is these two events.
Okay.
So the first event is called input began.
And you can get it by referencing the user input service.
And getting input began like this here.
And this event will fire when the player inputs anything.
So we press a press a key on a keyboard.
They press their mouse.
If they're on a different device, they might press on their tablet or press on their phone.
Or if they're on an Xbox, they might move the joystick, for example.
So any kind of possible inputs A2 will be picked up by this event here.
Okay.
So connect this with a function.
And there are two parameters involved here.
There's the inputs and there's a game processed event.
I'm gonna explain what the game process event is a bit later on okay.
But this is the layout of the event event here.
Okay.
Um, so let's just print input.
Received.
And let's see what happens when I go and play the game.
Okay, so I will load in here and already inputs being received.
I'm doing anything.
Of course you won't be able to tell here, but I'm moving around.
I'm using this here.
I'm doing anything and it's collecting input.
Any kind of key, anything.
It's collecting that input.
Okay.
Um, but obviously we want this to be a bit more precise.
Okay.
Um, we don't just want to detect when they, when, um, any kind of input has been detected has been
pressed.
We want to find out when a specific type of input has been used.
For example, a certain key on the keyboard, a left click, a touch on the screen, or a on a mobile
device.
We want to get more specific with this, and this is where the inputs parameter comes in.
The input parameter will store the type of input the player used, and we use that basically to say
to figure out what input type was received okay.
So we're going to use some if statements for these ones and basically say um, well we want to check
what type of input it was first as a different types of input.
And once I once I write it down, you'll be able to see a bit different types of input.
So if input dot and we want to say dot user input type, these are the different types of input being
performed.
And you can see here some of the options it gives you.
So for example the input via the mouse did the input via the keyboard via a gamepad via touchscreen.
Okay.
So these are all the different options you can have of input input dot user input type.
And remember if statements to equal signs okay.
Um and this is an enum value.
And it's just that up there we want to use enum dot use input type.
And here we have a different user types you can get.
So keyboard mouse button one which is your left click focus um which is when um.
Client regaining focus of the Roblox window.
Basically, when you open up the Roblox window again, that's not really used too much.
Mouse button two is the right mouse button.
Any mouse movements that you receive?
Um, none.
Which I think is basically useless.
Gyro is the gyroscope of mobile device.
So moving around your phone or moving around your tablet.
Touch.
So tap on the screen of a mobile device.
Um, you got gamepads as well if you use a gamepads.
Text inputs.
Mouse wheel.
Um.
Any input methods?
Mouse button three or middle mouse button.
Accelerometer for mobile device and all other kind of stuff.
So there's tons of different input types you can receive.
Again, just like with train service, there's lots of options.
So honestly try a lot of these out okay.
Um, I'm going to show you a couple of these.
But again it's very worth you taking a look at some other ones.
And if you do have an Xbox, maybe try out some of the um, gamepads and other Xbox uses as well on
this.
Okay.
Um, so let's choose um, mouse button one to start off with.
Okay.
So if the input type was mouse button one, then we don't need to do anything else after that, you
know.
But when this occurs Prince um mouse button.
Left.
Click like that.
There.
And that's it.
Okay.
Um, that if statement will filter out any other type of input and make sure that they've left click
their mouse.
Okay.
And obviously I could go and just play the game and um, and whenever I left click it will tell me where
I've left click okay.
So this might be good for like a left click based game.
Maybe like you're trying to left click any things come up on the screen or things like that.
So nice and useful that one.
Okay.
And the majority of other input types are just that simple.
You would just check the input type and then if that was successful then run whatever code you want.
Okay.
Um, but when we look at the keyboard specifically, it's a bit more to do a bit, as we don't just
want to know that the player inputted via their keyboard.
Okay, we want to know a bit more specific.
What actual key did they press?
And we can figure that out by using another if statement, which is basically checking the key code
used of the input.
So we can do that by checking if inputs or inputs dot key code, which is the key that was pressed.
Is equal to.
We can use an enum for this.
So is enum dot key code dot.
And here you can see all the different keys that are used.
So if I just keep scrolling down for a little bit, you can see there are literally every single possible
key you could pick from.
So it's so much more than just left clicking or pressing E.
There are so many more.
Even world keys.
World keys like additional keys that you could use there.
So generally not used.
Don't really.
I don't really know why they're here, but there are just so many options really that you could pick
from um right parentheses right bracket quoted double.
So this one here, there's just so many that most notably John majority of these you never use really.
But they are here okay.
Um so let's use E for example.
That's quite commonly used.
So e like that they're okay.
If they press E then it just prints um e key o.
Pressed.
Down.
Okay, let's just go into the game.
Take a look and yep, we press E!
It will let us know if the key was pressed down.
Okay.
And of course you can do this with any other letter on your keyboard okay.
You might be thinking, well, why do we need to check this and then check this?
Why can't we just check this and ignore this?
Well, the issue is if you don't first check that the input type was a keyboard.
And let's say they pressed on the mouse button, it will try to find a key code of the mouse button
which there isn't one.
So you do first have to check.
And fortunately that it was a keyboard before you check the key code.
So you can't include these in one if statement or anything like that has to be two.
Okay.
And that's pretty much it.
Okay.
There are infinite amount of users uses for this.
So please try out and experiment with this okay.
But that's just a very basics really of user input service.
Okay.
Um, the other event which I think is quite useful is use input service or input ended.
This is very similar, but this is basically when the input stops.
So input began is when the input is first received and input ended with an input stops.
This can be quite useful if you want to hold down a certain key you can use input began to detect when
A started.
The input and input ended when they finally let go of the key.
Let's say it was a stiff door you're trying to open, and the player has to hold down a button for a
few seconds in order to kind of push and open it.
That's used for quite a lot of popular games.
Um, you could definitely use a combination of those two, but everything else is complete.
Completely the same.
Okay, now there's one thing I didn't mention here, which is about the game processed event, and this
is just a boolean.
So let's have a go at printing the game processed event just to see what actually it is.
Okay.
So when I play the game and I go into the game like this here and I press E, it just prints false.
Okay.
But if I go to the chat and type hello.
It prints.
True.
So when I typed e when I was in, when I was, when I was chatting with people, it was true.
But when I'm doing it elsewhere, it's false.
Um, now basically what we're getting processed event is it detects whether the inputs, um, occurred.
Um.
You could say, however, if they meant to input it, or if or if it occurred using other guys or other
kind of services.
So if it is this case, if they press E, it opens something.
For example, if it's typing hello, hello in the chat, you don't want it to open that because they
don't mean to press that key.
Um, also I think like text boxes in GUIs would also work as well.
Game precisamente ruberry would be true and so on.
Okay, so a game processed event is basically an input which was generated from something else, not
a player actually meaning to input it.
So it's very handy to also check and not game processed event like this here, just to make sure the
term that input, they meant to input it and it wasn't an accidental input or just them doing it using
something else basically.
Okay.
So definitely want to use and to check before you run any code.
Okay.
Cool.
So that's pretty much it for user input service.
Okay.
There are some other things I want to just quickly show you with this and which are free extra kind
of details.
You can use user input service colon get mouse location.
And this will basically return the current screen location of the player's mouse.
And this is and this is using scale.
So if it's zero zero it's in the top left corner.
And if it's one one it will be in the bottom right corner.
This can be really useful for all kinds of things.
Um, maybe if you have a tool which launches like a grenade for example.
Okay.
Um, if you want the grenade to launch over there, you can left click and using some code for grenade
can launch over there.
And rather than launching in the center, okay.
And a lot of other kind of uses like that there.
Um, so that one's very useful okay.
Other ones you can use user input service dot is key or sorry colon is key down.
This will basically check if a certain key is currently down.
So a quicker way of using the key code if you're not wanting as much of the functionality in uses of
input began events and also user input service dot mouse behavior, you can use this to basically lock
the mouse.
Okay, so perhaps you're in a cutscene or something.
Or perhaps, um, a player is I don't know, maybe you've like, the player is being frozen by some
effect and you're not allowed to move for a few seconds.
You can lock their mouse by just writing enemy mouse behavior dot lock center, which will lock the
mouse in place.
And if I just quickly show you what that might look like when I join the game.
Um.
My mouse is here is.
Oh, I didn't fill in any things.
Let me just delete those two.
Now, I've already explained them and shove it out one more time.
And oh, it's not frozen.
Okay.
Might be because maybe to wait a few seconds.
Just because to give me time to load in and things like that.
If not, I'm not too sure why it's not working.
Um, but.
Oh, there we go.
Is now completely frozen like that.
I'm trying to move it, but it is just not working.
Okay.
Um, cool.
Um, how am I gonna move this out of the way?
Because now my mouse is stuck.
How am I going to stop this?
I think if you do it hard enough, it will glitch.
Yeah.
So.
Okay, I managed to stop that.
But um, yeah, so pretty cool as well for lots of different styles of game.
If you ever are wanting to lock the mouse, okay, but yeah, Use input service is a really cool service
and I think you have lots of fun with it.
Um, now, I haven't shown you many applications of this, and there's a very important reason as to
why.
Okay.
Um, which is that we're doing this locally and most of our applications for this, you're going to
want to do on the server.
And, um, so it becomes a bit tricky because you're writing code on a local script, which you then
want to, um, but you want some of that code to also run on the server.
So we're going to figure out how to overcome that in the next lecture or the next two lectures.
When we look at remote events and remote functions, these are very key and can really open up a lot
of possibilities and a lot of things that you previously wouldn't have been able to make.
Because of this, you will now understand how to how to do okay.
So have a play around and experiment a bit with user input service and join me in the next lesson.
We're going to have a look then at remote events.